package com.zhongkai.web.control.declare;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.code.TCsXtcs;
import com.zhongkai.model.config.User;
import com.zhongkai.model.declare.TSbCcssbxx;
import com.zhongkai.model.declare.TSbCcsspxx;
import com.zhongkai.service.declare.ZzzdspCancelService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.DateTool;

@Controller
public class ZzzdspCancelControll {
	private ZzzdspCancelService zzzdspCancelService;
	
	@Resource
	public void setZzzdspCancelService(ZzzdspCancelService zzzdspCancelService) {
		this.zzzdspCancelService = zzzdspCancelService;
	}



	//自助终端税票作废
	@RequestMapping(value="/system/ticket/zzzdspCancel_exec.do")
	public String zzzdSpCancel(String dqsphm,String dqzb,HttpServletRequest request,HttpServletResponse response) throws Exception {
		PrintWriter out=response.getWriter();
		User user=(User)request.getSession().getAttribute("user");
		String identifier=user.getUserIdentifier();
		TCsXtcs tCsXtcs=(TCsXtcs)zzzdspCancelService.getSingle(TCsXtcs.class, "csz", identifier);
		//02票证类型为电子税票
		List<TSbCcsspxx> tSbCcsspxxList=(List<TSbCcsspxx>)zzzdspCancelService.select("from TSbCcsspxx where sphm=? and zb=? and pzzlDm=?", new Object[]{dqsphm,dqzb,"02"});
		TSbCcsspxx tSbCcsspxx=tSbCcsspxxList.get(0);
		if(tSbCcsspxx==null){
			//税票不存在
			out.write(CheckWord.showMsg("无税票信息!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		if(tSbCcsspxx.getJbXh()!=null){
			//结报序号不为空,已结报
			out.write(CheckWord.showMsg("税票已结报!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		if(!"02".equals(tSbCcsspxx.getPzztDm())){
			//不为填开状态
			out.write(CheckWord.showMsg("该税票已作废或者为空白!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		if(tSbCcsspxx.getDzpjh()==null){
			out.write(CheckWord.showMsg("非电子申报方式，请转到作废界面进行作废!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		
		//取得一条申报信息记录
		TSbCcssbxx tSbCcssbxx=(TSbCcssbxx)zzzdspCancelService.getSingle(TSbCcssbxx.class, "spXh", tSbCcsspxx.getSpXh());
		//取得当前所有申报信息里面的最大时间
		Date dateMaxCurr=(Date)zzzdspCancelService.findBySql("select max(ssqq_rq) from t_sb_ccssbxx where ccdjh="+tSbCcssbxx.getCcdjh()+" and sp_xh="+tSbCcsspxx.getSpXh());

		//取得所有申报信息最大的所属期时间
		Date dateMax=(Date)zzzdspCancelService.findBySql("select max(ssqq_rq) from t_sb_ccssbxx where ccdjh="+tSbCcssbxx.getCcdjh());
		if(dateMax.getTime()>dateMaxCurr.getTime()){
			out.write(CheckWord.showMsg("不能作废,请检查当前号码为"+tSbCcsspxx.getSphm()+"的税票是否为最新税票!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		
		if(!"0002".equals(tCsXtcs.getCsDm())&&!identifier.equals(tSbCcsspxx.getCzrDm())){
			//只能操作自己的
			out.write(CheckWord.showMsg("您不能能操作此税票!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
			return null;
		}
		
		zzzdspCancelService.zzzdSpCancelSave(tSbCcsspxx,identifier);
		out.write(CheckWord.showMsg("作废操作成功!", "/system/ticket/zzzdspCancelform.jsp?sphm="+dqsphm,request));
		return null;
		
	}
}
